package TestHeap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 张杰
 * Date: 2022-03-06
 * Time: 17:59
 */
public class TestHeap {
    public int[] elem;
    public int usedSize;

    public TestHeap() {
        this.elem = new int[10];
    }

    /*
     * @author zj
     * @date 2022/3/7
     * @param parent  每棵树的根节点
     * @param len  每棵树调整的结束位置
    */
    public void shiftDown(int parent,int len) {
        int child =  2*parent + 1;
        while(child < len) {
            if(child+1 < len && elem[child] < elem[child+1]) {
                child++;
            }
            if(elem[child] > elem[parent]) {
                int tmp = elem[child];
                elem[child] = elem[parent];
                elem[parent] = tmp;
                parent = child;
                child = 2*parent+1;
            }else {
                break;
            }
        }
    }
    public void creatBigHeap(int[] array) {
        for(int i = 0; i < array.length;i++) {
            elem[i] = array[i];
            usedSize++;
        }
        for(int parent = (usedSize-1-1)/2; parent >= 0; parent--) {
            shiftDown(parent,usedSize);
        }
    }
}
